# -*- coding:utf-8 -*-

menu = {
    '北京':{
        '朝阳':{
            '国贸':{
                'CICC':{},
                'HP':{},
                "中国银行":{},
                'CCTV':{},
            },
            '望京':{
                '陌陌':{},
                '奔驰':{},
                '360':{},
            },
            '三里屯':{
                '优衣库':{},
                'apple':{},
            },
        },
        '昌平':{
            '沙河':{
                '庆丰包子':{},
                "阿泰包子":{},
            },
            '天通苑':{
                '链家':{},
                "我爱我家":{},
            },
            '回龙观':{},
        },
        '海淀':{
            '五道口':{
                '谷歌':{},
                "网易":{},
                "Sohu":{},
                "Sogo":{},
                "快手":{},
            },
            '中关村':{
                "youku":{},
                "Iqiyi":{},
                "汽车之家":{},
                "新东方":{},
                "QQ":{},
            }
        },
    },
    '上海':{
        "浦东":{
            "陆家嘴":{
                "CICC":{},
                "高盛":{},
                "摩根":{},
            },
            "外滩":{},
        },
        "闵行":{},
        "静安":{},
    },
    '山东':{
        "济南":{},
        "德州":{
            "乐陵":{
                "丁务镇":{},
                "城区":{},
            },
            "平原":{},
        },
        "青岛":{},
    },
}


'''
1. 可以一层一层的进入到所有层.
2. 可以在每层返回上一层.
3. 可以在任意层退出 主菜单.
'''

"""
back_flag = False
exit_flag = False

while not back_flag and not exit_flag:
    for key in menu:  # 北京
        print(key)  

    choice = input("请输入城市(1层):").strip()
    if choice == "q":
        exit_flag = True
    if choice in menu:

        while not back_flag and not exit_flag: # 让程序停在第2层
            for key2 in menu[choice]:
                print(key2)   # 昌平

            choice2 = input("请输入城市(2层):").strip()
            if choice2 == "b":
                back_flag = True
            if choice2 == "q":
                exit_flag = True

            if choice2 in menu[choice]:
                while not back_flag and not exit_flag: # 让程序停在第3层
                    for key3 in menu[choice][choice2]:
                        print(key3)  # 沙河

                    choice3 = input("请输入城市(3层):").strip()
                    if choice3 == "b":
                        back_flag = True
                    if choice3 == "q":
                        exit_flag = True
                    if choice3 in menu[choice][choice2]:
                        while not back_flag and not exit_flag:
                            for key4 in menu[choice][choice2][choice3]:
                                print(key4)
                            choice4 = input("请输入城市(4层)[退出b]:").strip()
                            print("last level")
                            if choice4 == "b":
                                back_flag = True
                            if choice4 == "q":
                                exit_flag = True
                            
                        else:
                            back_flag = False
                else:
                    back_flag = False
        else:
            back_flag = False
"""


current_layer = menu   # 实现动态循环
# parent_layer = menu
parent_layers = []  # 保存所有父级,最后一个元素永远都是父亲级.

while True:
    for key in current_layer:
        print(key)
    choice = input("选择城市:").strip()
    if len(choice) == 0:continue
    if choice in current_layer: 
        parent_layers.append(current_layer)  #在进入下一层之前,把当前曾(也就是下一层父级,)追加到列表中.下一次loop,当用户选择b的时候,就可以直接取列表的最后一个值出来就ok了.
        current_layer = current_layer[choice]  #改成了子层    
    elif choice == "b":
        if parent_layers:
            current_layer = parent_layers.pop()
            # 取出列表的最后一个值,因为它就是当前层的父级.
    else:
        print("无这个城市")
该学python全栈学习: day14- 基础篇了.



                    


